﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using BCEntity.CRM.CustomerFollowUpRecord;
using Dapper;
using System.Data;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.CRM.CustomerFollowUpRecord
{
    /// <summary>
    /// 客户跟进记录表
    /// </summary>
    public class CustomerFollowUpRecordData : ICustomerFollowUpRecordData
    {
        #region var
        private IDatabaseContext database;
        private string QUERY = @"SELECT 
                                      `CustomerFollowUpRecordId`,
                                        CompanyId,
                                      `CustomerInfoId`,
                                      `UserName`,
                                      `Remark`,
                                      `CreateTime`,
                                      `AdminId`,
                                      `AdminFullName`,
                                      `FollowUpStatus`,
                                      `CreateAdminId`,
                                      `CreateAdminFullName`,
                                      `ScheduleTime`,
                                      `TimeoutDate`,
                                      `IsTimeout`,
                                      `IsGivenUp`,
                                      `IsShake`,
                                      `CustomerGrade` 
                                    FROM `crm_customerfollowuprecord`";

        #endregion
        #region 构造函数
        public CustomerFollowUpRecordData(IDatabaseContext databaseContext)
        {
            this.database = databaseContext;
        }
        #endregion

        #region InsertAsync
        /// <summary>
        /// 添加新纪录
        /// </summary>
        /// <param name="CustomerFollowUpRecordEntity"></param>
        /// <returns></returns>
        public async Task<CustomerFollowUpRecordEntity> InsertAsync(CustomerFollowUpRecordEntity entity)
        {
            return await this.database.Master.InsertAsync(entity, database.Transaction);
        }
        #endregion
        
        #region updateAsync
        /// <summary>
        /// 根据主键ID更新记录
        /// </summary>
        /// <param name="CustomerFollowUpRecordEntity"></param>
        /// <returns></returns>
        public async Task<CustomerFollowUpRecordEntity> UpdateAsync(CustomerFollowUpRecordEntity entity)
        {
            return await this.database.Master.UpdateAsync(entity, database.Transaction);
        }
        #endregion

        #region Delete
        /// <summary>
        /// 删除指定ID的记录
        /// </summary>
        /// <param name="customerFollowUpRecordId"></param>
        /// <returns></returns>
        public async Task<bool> Delete(long customerFollowUpRecordId)
        {
            string fSql = "DELETE FROM crm_customerfollowuprecord WHERE CustomerFollowUpRecordId = ?CustomerFollowUpRecordId"; 
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?CustomerFollowUpRecordId", customerFollowUpRecordId, DbType.Int64);
            return await this.database.Master.ExecuteAsync(fSql, parameters, database.Transaction)>0;
        }
        #endregion

        #region GetById
        /// <summary>
        /// 获取指定ID的记录
        /// </summary>
        /// <param name="customerFollowUpRecordId"></param>
        /// <returns></returns>
        public async Task<CustomerFollowUpRecordEntity> GetById(long customerFollowUpRecordId)
        {
            string fSql = QUERY + " WHERE  CustomerFollowUpRecordId = ?CustomerFollowUpRecordId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?CustomerFollowUpRecordId", customerFollowUpRecordId, DbType.Int64);  
            return await database.Slave.QueryFirstOrDefaultAsync<CustomerFollowUpRecordEntity>(fSql, parameters);
        }
        #endregion

        #region GetList ByPage
        public async Task<(int ccCount,IEnumerable<CustomerFollowUpRecordEntity>)> GetListAsync(int companyId,int ccPageNo,int ccPageSize)
        {
            string where = $" WHERE CompanyId={companyId} ";
            string fSql = string.Format( QUERY + where + "  ORDER BY  CustomerFollowUpRecordId DESC LIMIT {0},{1} ; ", (ccPageNo-1)*ccPageSize, ccPageSize);
            string fSqlCount = string.Format("SELECT  COUNT(1) FROM crm_customerfollowuprecord " + where);
            var fRet =  await this.database.Slave.QueryMultipleAsync<CustomerFollowUpRecordEntity,int>(fSql + fSqlCount, null);
            return (fRet.Item2, fRet.Item1.ToList());

        }
        #endregion

        #region GetLastRecord by-CustomerID
        public async Task<CustomerFollowUpRecordEntity> GetLastRecord(long ccCustomerInfoId)
        {
            string fSql = QUERY + " WHERE CustomerInfoId = ?CustomerInfoId ORDER BY CustomerFollowUpRecordId DESC LIMIT 0,1";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?CustomerInfoId", ccCustomerInfoId, DbType.Int64);
            var a = await this.database.Slave.QueryFirstOrDefaultAsync<CustomerFollowUpRecordEntity>(fSql, parameters);
            return a;
        }
        #endregion

        #region GetListByCustomer ByPage
        public async Task<(int ccCount,IEnumerable<CustomerFollowUpRecordEntity>)> GetListByCustomerAsync(long ccCustomerInfoId, int ccPageNo, int ccPageSize)
        {
            string fSqlCount = string.Format(" SELECT COUNT(1) FROM crm_customerfollowuprecord WHERE CustomerInfoId = {0}  ; ", ccCustomerInfoId);
            string fSql = string.Format(" SELECT  * FROM crm_customerfollowuprecord WHERE CustomerInfoId = {2} ORDER BY  CustomerFollowUpRecordId DESC LIMIT {0},{1} ; ", (ccPageNo - 1) * ccPageSize, ccPageSize, ccCustomerInfoId);
            var fRet = await this.database.Slave.QueryMultipleAsync<CustomerFollowUpRecordEntity,int>(fSql + fSqlCount);
            return (fRet.Item2, fRet.Item1.ToList());
        }
        #endregion

    }
}